/**
 * rainbow.com Inc.
 * Copyright (c) 2014-2015 All Rights Reserved.
 */
package com.rainbow.bam.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Page;

/**
 * 分页请求响应结果对象
 * 
 * @author HarrisonHan
 * @version $Id: PaginationResponse.java, v 0.1 2015年10月26日 下午12:56:06 HarrisonHan Exp $
 * @param <T>
 */
public class PaginationResponse<T> implements Serializable {

    /**  */
    private static final long serialVersionUID = 1115676724739520700L;
    /**
     * 页码分割阀值
     */
    private static int        SPLIT_PAGE_NUM   = 5;                   //页码按照5个为一组进行拆分

    /**
     * 当前页码
     */
    private int               currentPage;
    /**
     * 每页大小
     */
    private int               pageSize;
    /**
     * 总页数
     */
    private int               totalPages;
    private int               numberOfElements;
    /**
     * 总记录数
     */
    private long              totalElements;
    /**
     * 是否有上一页
     */
    private boolean           hasPreviousPage;
    /**
     * 是否是第一页
     */
    private boolean           isFirstPage;
    /**
     * 是否有下一页
     */
    private boolean           hasNextPage;
    /**
     * 是否是最后一页
     */
    private boolean           isLastPage;
    /**
     * 是否有内容
     */
    private boolean           hasContent;
    /**
     * 结果集合
     */
    private List<T>           content;
    /**
     * 页码集合
     */
    private List<Integer>     pages;

    /**
     * 构造方法
     */
    @SuppressWarnings("hiding")
    public <T> PaginationResponse(final Page<T> page, final Class<? extends T> clz) {
        this.currentPage = page.getNumber();
        this.pageSize = page.getSize();
        this.totalPages = page.getTotalPages();
        this.numberOfElements = page.getNumberOfElements();
        this.totalElements = page.getTotalElements();
        this.hasPreviousPage = page.hasPrevious();
        this.isFirstPage = page.isFirst();
        this.hasNextPage = page.hasNext();
        this.isLastPage = page.isLast();
        if (page.getContent() != null && page.getContent().size() > 0) {
            this.hasContent = true;
        }
        this.pages = pageNumbers(page.getTotalPages(),page.getNumber());
    }

    /**
     * 根据页码数获取页码List
     * 
     * @param totalpages 总页码数
     * @return
     */
    public static List<Integer> pageNumbers(int totalpages, int currentpage) {
        List<Integer> pageNumList = new ArrayList<Integer>();
        int beginRange = 1;
        int endRange = SPLIT_PAGE_NUM;
        if (totalpages <= SPLIT_PAGE_NUM) {
            for (int i = 1; i <= totalpages; i++) {
                pageNumList.add(i);
            }
        } else {
            int value = currentpage / SPLIT_PAGE_NUM;
            if (value > 0) {
                beginRange = (beginRange + (value * SPLIT_PAGE_NUM)) ;
                endRange = endRange + (value * SPLIT_PAGE_NUM);
                if (endRange > totalpages) {
                    endRange = totalpages;
                }
            }
            for (int i = beginRange; i <= endRange; i++) {
                pageNumList.add(i);
            }
        }
        return pageNumList;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getNumberOfElements() {
        return numberOfElements;
    }

    public void setNumberOfElements(int numberOfElements) {
        this.numberOfElements = numberOfElements;
    }

    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public boolean isFirstPage() {
        return isFirstPage;
    }

    public void setFirstPage(boolean isFirstPage) {
        this.isFirstPage = isFirstPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public boolean isLastPage() {
        return isLastPage;
    }

    public void setLastPage(boolean isLastPage) {
        this.isLastPage = isLastPage;
    }

    public boolean isHasContent() {
        return hasContent;
    }

    public void setHasContent(boolean hasContent) {
        this.hasContent = hasContent;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public List<Integer> getPages() {
        return pages;
    }

    public void setPages(List<Integer> pages) {
        this.pages = pages;
    }
}
